<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="function">
                            <option value="">Job Function</option>
                            {volist name="function" id="vo"}
                            <option value="{$vo}">{$vo}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="age">
                            <option value="">年龄段</option>
                            <option value="1">25岁以下</option>
                            <option value="2">25-29岁</option>
                            <option value="3">30-39岁</option>
                            <option value="4">40-49岁</option>
                            <option value="5">50岁以上</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="course_type">
                            <option value="">课程类型</option>
                            <option value="1">SPARK E-learning</option>
                            <option value="3">轻学堂</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="key" placeholder="课程标题" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="time1" id="time" placeholder="日期范围" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;

        laydate.render({
            elem: '#time'
            ,range: true //或 range: '~' 来自定义分割字符
            ,trigger: 'click'
        });

        let cols = [
            [{
                    title: 'CourseId',
                    field: 'id',
                    align: 'center',
                },
                {
                    title: '课程标题',
                    field: 'title',
                    align: 'center',
                },
                {
                    title: '课程类型',
                    field: 'course_type',
                    align: 'center',
                },
                {
                    title: 'CTR',
                    field: 'ctr',
                    align: 'center',
                    sort: true
                },
                {
                    title: '推荐曝光量',
                    field: 'exposure',
                    align: 'center',
                    sort: true
                },
                {
                    title: '推荐点击量',
                    field: 'click',
                    align: 'center',
                    sort: true
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/CourseData/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }, {
                title: '提示' //标题
                ,layEvent: 'tips' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-tips' //图标类名
            }]
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        //监听排序事件
        table.on('sort(user-table)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var field = form.val('reload');
            table.reload('user-table', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                    ,function: field.function
                    ,age: field.age
                    ,course_type: field.course_type
                    ,time1: field.time1
                    ,key: field.key
                }
            });
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'tips') {
                window.tips();
            } else if (obj.event === 'export') {
                window.export();
            }
        });

        window.tips = function() {
            layer.open({
                type: 1,
                title:'提示',
                shade: 0.1,
                area: [common.isModile()?'100%':'520px', common.isModile()?'100%':'240px'],
                content: '<div style="margin-bottom: 20px"><strong><i class="layui-icon layui-icon-rate"></i> CTR：</strong>点击量除以曝光量</div>\n' +
                    '<div style="margin-bottom: 20px"><strong><i class="layui-icon layui-icon-rate"></i> 推荐曝光量：</strong>在推荐课程中被展示的次数</div>\n' +
                    '<div style="margin-bottom: 20px"><strong><i class="layui-icon layui-icon-rate"></i> 推荐点击量：</strong>在推荐课程中被点击的次数</div>'
            });
        };


        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('export')}?function="+field.function+"&age="+field.age+"&course_type="+field.course_type+"&time1="+field.time1+"&key="+field.key, "_blank");
            });
        };

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
